package com.hdoit.ares.web.acc.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acc.model.GoldenBeanDetail;
import com.hdoit.ares.logical.acc.service.IGoldenBeanDetailService;
import com.hdoit.ares.logical.viewmodel.acc.FindGoldenDetail;
import com.tennetcn.component.webapi.BaseApi;
import com.tennetcn.component.webapi.ResponseModel;
import com.tennetcn.component.webapi.ResponseStatus;

@Controller
@RequestMapping(value = "/api/v1/goldenBeanAres/",method = {RequestMethod.GET,RequestMethod.POST},produces="application/json;charset=utf-8")
@Api(value="/api/v1/goldenBeanAres/")
@CrossOrigin(origins = "*",maxAge = 3600)
public class goldenBeanAresApi extends BaseApi{

	private IGoldenBeanDetailService goldenBeanDetailService;
	public IGoldenBeanDetailService getGoldenBeanDetailService() {
		return goldenBeanDetailService;
	}
	@Resource
	public void setGoldenBeanDetailService(IGoldenBeanDetailService goldenBeanDetailService) {
		this.goldenBeanDetailService = goldenBeanDetailService;
	}

	private final String bonusDetailListApi="bonusDetailList";
	private final String goldenApi="golden";
	
	@ResponseBody
	@RequestMapping(value = bonusDetailListApi)
	@ApiOperation(notes = bonusDetailListApi, httpMethod = "POST", value = "获取金豆详情列表信息")
	public String bonusDetailList(FindGoldenDetail findGoldenDetail,
			@ApiParam("第几页,必选") @RequestParam(value="pageNum",required=true)Integer pageNum,
			@ApiParam("每页数量") @RequestParam(value="length",required=true)Integer length
			/*@ApiParam("查询的开始时间") @RequestParam(value="strDate",required=false)String strDate,
			@ApiParam("查询的结束时间") @RequestParam(value="endDate",required=false)String endDate*/) {
		
		/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
	    try {
	    	if(!StringUtils.isEmpty(strDate)){
	    		findGoldenDetail.setStartDate(sdf.parse(strDate));
	    	}
	    	if(!StringUtils.isEmpty(endDate)){
	    		findGoldenDetail.setEndDate(sdf.parse(endDate));
	    	}
	    	
		} catch (ParseException e) {
			e.printStackTrace();
		}*/
	    
		ResponseModel rm=new ResponseModel();
		String custId=getLoginId();
		int start=(pageNum-1)*length;
		List<GoldenBeanDetail> goldenBeanDetails = goldenBeanDetailService.queryGoldenList(findGoldenDetail, custId,start,length);
		 
		if(goldenBeanDetails.size()>0){
			rm.setStatus(ResponseStatus.success);
			rm.setMessage("ok");
			List<String> params = new ArrayList<String>();
			Field[] fields = GoldenBeanDetail.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			
			rm.putList("goldenBeanDetail", goldenBeanDetails, params);
		}else{
			rm.setMessage("没有查询到数据");
		}

		Double incomeMoney = null;
		GoldenBeanDetail goldenBeanDetail1 = goldenBeanDetailService.querySumIncomeMoney(findGoldenDetail, custId);
		if (goldenBeanDetail1 == null) {
			incomeMoney = 0.00;
		} else {
			incomeMoney = goldenBeanDetail1.getIncomeMoney();
		}
		if(incomeMoney==null){
			rm.setMessage("总收入计算错误");
		}else{
		    rm.put("incomeMoney", incomeMoney);
		}

		Double expendMoney = null;
		GoldenBeanDetail goldenBeanDetail2 = goldenBeanDetailService.querySumExpendMoney(findGoldenDetail, custId);
		if (goldenBeanDetail2 == null) {
			expendMoney = 0.00;
		} else {
			expendMoney = goldenBeanDetail2.getExpendMoney();
		}
		if(incomeMoney==null){
			rm.setMessage("总支出计算错误");
		}else{
			rm.put("expendMoney", expendMoney);
		}
		
		return rm.toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = goldenApi)
	@ApiOperation(notes = goldenApi, httpMethod = "POST", value = "选择的金豆详情页面")
	public String bonus(String id) {

		ResponseModel rm=new ResponseModel();
		if(StringUtils.isEmpty(id)){
			rm.setMessage("id为空，请输入id");
		}
		
		GoldenBeanDetail goldenBeanDetail = goldenBeanDetailService.queryGoldenBean(id);
		rm.put("goldenBeanDetail", goldenBeanDetail);

		return rm.toJson();
	}

}
